/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sxu.shadowdrawabledemo.slice;

import com.sxu.shadowdrawable.ShadowDrawable;
import com.sxu.shadowdrawabledemo.MyApplication;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.DisplayManager;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * Description:
 *
 * Author: Freeman
 *
 * Date: 2019/2/12
 *
 * Copyright: all rights reserved by Freeman.
 *******************************************************************************/
public class RecycleExampleSlice extends Fraction {
    private ListContainer recyclerView;
    private List<String> dataList = new ArrayList<>();
    private static final int COLUMN_COUNT = 3;
    private static final int DATALIST_SIZE = 20;
    private static final int PADDING_SIZE = 10;
    private static final int PROPORTION = 3;
    private static final int RANDOM_PROPORTION = 300;
    private static int WIDTH;
    private static final int TEXT_SIZE = 26;
    private static final int HEIGHT = 120;
    private static final int SHAPERADIUS = 8;
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        recyclerView = new ListContainer(MyApplication.getInstance());
        TableLayoutManager tableLayoutManager = new TableLayoutManager();
        tableLayoutManager.setColumnCount(COLUMN_COUNT);
        tableLayoutManager.setOrientation(Component.HORIZONTAL);
        recyclerView.setLayoutManager(tableLayoutManager);
        recyclerView.setLongClickable(false);
        return recyclerView;
    }

    @Override
    protected void onActive() {
        super.onActive();

        for (int i1 = 0; i1 < DATALIST_SIZE; i1++) {
            dataList.add("Item_" + i1);
        }
        recyclerView.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object getItem(int i) {
                return dataList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                Text text = new Text(getContext());
                int padding = PADDING_SIZE * PROPORTION;
                text.setTextAlignment(TextAlignment.CENTER);
                text.setText(dataList.get(i));
                text.setWidth(WIDTH / PROPORTION);
                text.setTextColor(Color.WHITE);
                text.setTextSize(TEXT_SIZE);
                SecureRandom random = new SecureRandom();
                text.setMinHeight((int) (HEIGHT * PROPORTION + random.nextFloat() * RANDOM_PROPORTION));
                ShadowDrawable.setShadowDrawable(text, Color.getIntColor("#2979FF"), SHAPERADIUS * PROPORTION,
                        Color.getIntColor("#ee7C4DFF"), padding, 0, 0);
                return text;
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        WIDTH = DisplayManager.getInstance().
                getDefaultDisplay(MyApplication.getInstance()).get().getRealAttributes().width;
        int height = DisplayManager.getInstance().
                getDefaultDisplay(MyApplication.getInstance()).get().getRealAttributes().height;
        if (WIDTH > height) {
            WIDTH = DisplayManager.getInstance().
                    getDefaultDisplay(MyApplication.getInstance()).get().getRealAttributes().width - 255;
        }
        if (height > WIDTH) {
            WIDTH = DisplayManager.getInstance().
                    getDefaultDisplay(MyApplication.getInstance()).get().getRealAttributes().width;
        }
    }
}
